﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using AutoMapper;

namespace Bot.Domain.Categories.Service
{
    public class AnswerInChannelFormDomainService : IAnswerInChannelFormDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, AnswerInChannelForm> _repository;
        public IMapper Mapper { get; set; }

        public AnswerInChannelFormDomainService(IRepository<Guid, AnswerInChannelForm> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public AnswerInChannelForm Create(AnswerInChannelForm form)
        {
            if(form.Id == Guid.Empty)
            {
                form.Id = Guid.NewGuid();
            }

            if(form.Fields!=null&& form.Fields.Count > 0)
            {
                IAnswerInChannelFormFieldDomainService answerInChannelFormFieldDomainService = _container.Resolve<IAnswerInChannelFormFieldDomainService>();
                foreach(var field in form.Fields)
                {
                    field.FormId = form.Id;
                    field.Id = Guid.NewGuid();
                    answerInChannelFormFieldDomainService.Create(field);
                }
            }

            return _repository.Create(form);
        }

        public AnswerInChannelForm Get(Guid id, string include = "")
        {
            var form = _repository.Get(id);
            if (form != null && !string.IsNullOrEmpty(include)&&include.Split(',').Contains("fields"))
            {
                IAnswerInChannelFormFieldDomainService answerInChannelFormFieldDomainService = _container.Resolve<IAnswerInChannelFormFieldDomainService>();
                form.Fields = answerInChannelFormFieldDomainService.GetByFormId(form.Id, include).ToList();
            }
            return form;
        }

        public AnswerInChannelForm Update(AnswerInChannelFormUpdateBo bo)
        {
            AnswerInChannelForm answerInChannelForm = _repository.Get(bo.Id);
            IAnswerInChannelFormFieldDomainService answerInChannelFormFieldDomainService = _container.Resolve<IAnswerInChannelFormFieldDomainService>();

            var fieldIds = new List<Guid> { };

            if (bo.Fields != null && bo.Fields.Count > 0)
            {
                foreach (var fieldBo in bo.Fields)
                {
                    if(fieldBo.Id == Guid.Empty)
                    {
                        //新增
                        fieldBo.Id = Guid.NewGuid();
                        fieldBo.FormId = bo.Id;
                        var field = Mapper.Map<AnswerInChannelFormField>(fieldBo);
                        answerInChannelFormFieldDomainService.Create(field);
                        fieldIds.Add(fieldBo.Id);
                    }
                    else
                    {
                        //修改
                        fieldBo.FormId = bo.Id;
                        answerInChannelFormFieldDomainService.Update(fieldBo);
                        fieldIds.Add(fieldBo.Id);
                    }
                }
            }
            //删除
            var fields = answerInChannelFormFieldDomainService.GetByFormId(bo.Id);
            foreach(var filed in fields)
            {
                if (!fieldIds.Contains(filed.Id))
                {
                    answerInChannelFormFieldDomainService.Delete(filed.Id);
                }
            }

            answerInChannelForm.Id = bo.Id;
            answerInChannelForm.Title = bo.Title;
            answerInChannelForm.Message = bo.Message;
            answerInChannelForm.IfRequireConfirm = bo.IfRequireConfirm;

            return answerInChannelForm;

        }

        public IEnumerable<AnswerInChannelForm> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            AnswerInChannelForm answerInChannelForm = Get(id, "fields");
            if (answerInChannelForm != null)
            {
                if (answerInChannelForm != null && answerInChannelForm.Fields != null && answerInChannelForm.Fields.Count > 0)
                {
                    IAnswerInChannelFormFieldDomainService answerInChannelFormFieldDomainService = _container.Resolve<IAnswerInChannelFormFieldDomainService>();
                    foreach (var field in answerInChannelForm.Fields)
                    {
                        answerInChannelFormFieldDomainService.Delete(field.Id);
                    }
                }
                IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
                var intentAnswerInChannel = intentAnswerInChannelDomainService.GetByFormId(id);
                if (intentAnswerInChannel != null)
                {
                    intentAnswerInChannelDomainService.UpdateForFormId(intentAnswerInChannel.Id, Guid.Empty);
                }
                _repository.Delete(answerInChannelForm);

            }           
        }
        public AnswerInChannelForm GetByAnswerInChannelId(Guid answerInChannelId, string include = "")
        {
            IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
            var intentAnswerInChannel = intentAnswerInChannelDomainService.Get(answerInChannelId, "fields"+ include);

            return intentAnswerInChannel.Form;
        }
        public AnswerInChannelForm CreateWithAnswerInChannelId(Guid answerInChannelId, AnswerInChannelForm answerInChannelForm)
        {
            answerInChannelForm = Create(answerInChannelForm);
            IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();

            intentAnswerInChannelDomainService.UpdateForFormId(answerInChannelId, answerInChannelForm.Id);

            return answerInChannelForm;
        }
    }
}
